package cn.openea.perpartner.meily.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.ActivityProducts;
import cn.openea.perpartner.meily.web.entity.dto.ProductDto;
import cn.openea.perpartner.meily.web.entity.vo.ProductInfo;
import cn.openea.perpartner.meily.web.entity.vo.ProductVo;
import cn.openea.perpartner.meily.web.service.IActivityProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 活动商品控制层
 * @author Weird077
 * @since 2023-03-30 14:31:32
 */
@Api(tags = "活动商品")
@RestController
@RequestMapping("/activity/product")
public class ActivityProductsController extends BaseController {
    @Resource
    private IActivityProductsService productService;

    @ApiOperation("活动商品列表")
    @PostMapping("/list")
    public TableDataInfo selectProductList(@RequestBody ProductInfo product) {
        startPage();
        List<ProductVo> list = productService.selectProductList(product);
        return getDataTable(list);
    }
    @ApiOperation("小程序-活动商品列表")
    @PostMapping("/shelf/list")
    public TableDataInfo selectShelfProductList(@RequestBody ProductVo productVo) {
        startPage();
        List<ProductVo> list = productService.selectShelfProductList(productVo);
        return getDataTable(list);
    }

    @ApiOperation("活动商品详情")
    @GetMapping("{id}")
    public AjaxResult selectByProductId(@PathVariable Serializable id) {
        return success(productService.selectByProductId(id));
    }
    @ApiOperation("小程序-商品详情")
    @GetMapping("/get/{goodsNo}")
    public AjaxResult findGoodsNo(@PathVariable String goodsNo) {
        return success(this.productService.findByGoodsNo(goodsNo));
    }
    @ApiOperation("批量添加商品")
    @PostMapping("/batch/save")
    public AjaxResult batchInsertProduct(@RequestBody ProductDto dto) {
        String message = productService.batchInsertProduct(dto);
        return success(message);
    }

    @ApiOperation("保存活动商品")
    @PostMapping("/save")
    public AjaxResult insertProduct(@RequestBody ActivityProducts products) {
        if (products.getGoodsId() == null) {
            products.setCreateUser(UserContext.getUserId());
            products.setCreateTime(DateUtil.now());
        }
        products.setUpdateUser(UserContext.getUserId());
        products.setUpdateTime(UserContext.getUserId());
        return toAjax(productService.saveOrUpdate(products));
    }

    @ApiOperation("删除活动商品")
    @PostMapping("/delete")
    public AjaxResult batchRemove(@RequestBody List<String> idList) {
        return toAjax(productService.removeByIds(idList));
    }

    @ApiOperation("上架商品")
    @GetMapping("/shelf/{productId}")
    public AjaxResult shelf(@PathVariable Long productId) {

        return toAjax(productService.shelf(productId));
    }
    @ApiOperation("下架商品")
    @GetMapping("/down/{productId}")
    public AjaxResult down(@PathVariable Long productId) {

        return toAjax(productService.down(productId));
    }
    @ApiOperation("置顶商品")
    @GetMapping("/top/{productId}")
    public AjaxResult top(@PathVariable Long productId) {

        return toAjax(productService.top(productId));
    }
    @ApiOperation("取消置顶")
    @GetMapping("/top/cancel/{productId}")
    public AjaxResult topCancel(@PathVariable Long productId) {

        return toAjax(productService.topCancel(productId));
    }
    @ApiOperation("商品分类")
    @GetMapping("/category/{type}")
    public AjaxResult selectProductCategory(@PathVariable String type) {

        return success(productService.selectProductCategory(type));
    }

}

